import Viewer from "@/modules/Viewer";
import * as THREE from 'three';

export default class Sprite {
    viewer: Viewer;
    texture: string;
    pointPosition: THREE.Vector3;
    sprite: THREE.Sprite | null = null;

    constructor(viewer: Viewer, texture: string, pointPosition: THREE.Vector3) {
        this.viewer = viewer;
        this.texture = texture;
        this.pointPosition = pointPosition;
        this.initSprite();
    }

    private initSprite() {
        const path = `${import.meta.env.VITE_PUBLIC_BASE_PATH || ''}/skybox/${this.texture}`;
        const textureLoader = new THREE.TextureLoader();
        console.log(path)
        textureLoader.load(
            path,
            (texture) => {
                const spriteMaterial = new THREE.SpriteMaterial({
                    map: texture,
                    color: 0xffffff,
                    transparent: true,
                });

                this.sprite = new THREE.Sprite(spriteMaterial);
                this.sprite.scale.set(0.4, 0.4, 1);
                this.sprite.position.copy(this.pointPosition);
                this.viewer.scene.add(this.sprite);
            },
            undefined,
            (error) => {
                console.error("Failed to load sprite texture:", error);
            }
        );
    }

    public dispose() {
        if (this.sprite) {
            this.viewer.scene.remove(this.sprite);
            if (this.sprite.material.map) {
                this.sprite.material.map.dispose();
            }
            this.sprite.material.dispose();
            this.sprite = null;
        }
    }
}